package com.liyuan.yiqixing.controller;

import com.alibaba.fastjson.JSONObject;
import com.liyuan.yiqixing.model.Comment;
import com.liyuan.yiqixing.model.ResultJson;
import com.liyuan.yiqixing.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DiscussionItemController {
    @Autowired
    CommentService commentService;

    @PostMapping("/comment")
    public ResultJson<Object> comment(@RequestBody Comment comment){
        commentService.insertComment(comment);
        return new ResultJson<>(200,"评论成功！！",null);
    }

    @PostMapping("/discussionItem/retrieveComments")
    public ResultJson<Object> retrieveComments(@RequestBody String data){
        JSONObject jsonObject = JSONObject.parseObject(data);
        Integer type = jsonObject.getInteger("type");
        Integer targetId = jsonObject.getInteger("targetId");
        Comment[] comments =commentService.retrieveComments(type,targetId);
        return new ResultJson<>(200,"Retrieve comments success!!!",comments);
    }
}
